package game;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import graphisme.Checkers.Case;
import graphisme.Checkers.CheckersGraphicalInterface;
import player.Player;

public class Checkers extends SuperGame {

	Player gameWinner = null;
	boolean draw = false;
	CheckersGraphicalInterface graphicBoard = null;
	OutputStream out;
	BufferedWriter bufOut = null;
	String[] indiceTab;

	public Checkers(String tmpGame, Player player) {

		// call superclass' constructor
		super(tmpGame, player);

		// set player's number to 2
		setNbJoueur(2);
		setTpt(true);
	}

	// method handling actions on the board
	public void play() {

		// get last msg from the thread listenplayer
		String currentMsgP1 = playerList.get(0).getCurrentMsg();
		String currentMsgP2 = playerList.get(1).getCurrentMsg();
		
		// if both players have received the msg
		if (currentMsgP1.equals("color?") && currentMsgP2.equals("color?")) {
			
			// choice random of player who starts 
			// generate a number between 0 and 10
			int token = 0 + (int) (Math.random() * ((10 - 0) + 1));
			
			// 0 <= x <= 5 : player 1 starts, otherwise player2 starts
			if (token <= 5) {
				sendMsg("blanc", playerList.get(0).getBufOut());
				sendMsg("noir", playerList.get(1).getBufOut());
			} else {
				sendMsg("noir", playerList.get(0).getBufOut());
				sendMsg("blanc", playerList.get(1).getBufOut());
			}

			playerList.get(0).setCurrentMsg("");
			playerList.get(1).setCurrentMsg("");
		}
		
		// if player1 has made a move
		else if (currentMsgP1.contains("caseMove_")) {
			System.out.println(" P1 sent message to "
					+ playerList.get(1).getPseudo());
			sendMsg(currentMsgP1, playerList.get(1).getBufOut());
			playerList.get(0).setCurrentMsg("");
			playerList.get(1).setCurrentMsg("");
		}
		// if player2 has made a move
		else if (currentMsgP2.contains("caseMove_")) {
			System.out.println(" P2 sent message to "
					+ playerList.get(0).getPseudo());
			sendMsg(currentMsgP2, playerList.get(0).getBufOut());
			playerList.get(0).setCurrentMsg("");
			playerList.get(1).setCurrentMsg("");
		}
		// if message indicates white pawn lost
		else if (currentMsgP1.equals("Blanc Loose")
				|| currentMsgP2.equals("Blanc Loose")) {
			sendMsg("Blanc Loose", playerList.get(0).getBufOut());
			sendMsg("Blanc Loose", playerList.get(1).getBufOut());
			playerList.get(0).setCurrentMsg("");
			playerList.get(1).setCurrentMsg("");
		}
		// if message indicates black pawn lost
		else if (currentMsgP1.equals("Noir Loose")
				|| currentMsgP2.equals("Noir Loose")) {
			sendMsg("Noir Loose", playerList.get(0).getBufOut());
			sendMsg("Noir Loose", playerList.get(1).getBufOut());
			playerList.get(0).setCurrentMsg("");
			playerList.get(1).setCurrentMsg("");

		}

	}

	// display score board
	public void displayBoard(Socket socket) throws IOException {
		this.graphicBoard = new CheckersGraphicalInterface(9, socket);
		graphicBoard.setSocket(socket);
		
		// derterminate player pawn's color 
		graphicBoard.sendMsg("color?");
		
		//creation of the window
		JFrame f = new JFrame();
		f.setSize(600, 600);
		f.setLocationRelativeTo(null);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.add(this.graphicBoard);
		f.setVisible(true);
	}
	
	// methode refreshing board
	public void refreshBoard(String msg) {
		System.out.println("Message of board refreshed " + msg);
		if (msg.equals("noir")) {
			graphicBoard.setAuthorizedColor("noir");
			graphicBoard.createMSgBOX("You play in second");
		} else if (msg.equals("blanc")) {
			graphicBoard.setAuthorizedColor("blanc");
			graphicBoard.createMSgBOX("You start");
			graphicBoard.setYourTurn(true);
		}
		// if we should update the board
		else if (msg.contains("caseMove")) {
			indiceTab = null;
			indiceTab = msg.split("_");

			final Case case1 = graphicBoard.getCase(
					Integer.parseInt(indiceTab[1]),
					Integer.parseInt(indiceTab[2]));
			Case caseActive = null;
			caseActive = graphicBoard.getCase(Integer.parseInt(indiceTab[3]),
					Integer.parseInt(indiceTab[4]));

			graphicBoard.setCaseActive(caseActive);

			System.out.println(graphicBoard.getLigne(caseActive) + "--"
					+ graphicBoard.getColonne(caseActive));

			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					graphicBoard.play(case1);
				}
			});

			graphicBoard.setCaseTmpActive(null);
			graphicBoard.setYourTurn(true);
			graphicBoard.clearFrame();

		} 
		// messages at the end of the game
		else if (msg.equals("Blanc Loose")) {
			System.out.println("les blanc on loose");
			graphicBoard.clearFrame();
			if (graphicBoard.getAuthorizedColor().equals("blanc")) {
				graphicBoard.createMSgBOX("Yoo loose big noob");
			} else {
				graphicBoard.createMSgBOX("GG my dear");
			}
		} else if (msg.equals("Noir Loose")) {
			System.out.println("Les noir on loose");
			graphicBoard.clearFrame();
			if (graphicBoard.getAuthorizedColor().equals("noir")) {
				graphicBoard.createMSgBOX("Yoo loose big noob");
			} else {
				graphicBoard.createMSgBOX("GG my dear");
			}
		}

	}

}
